package com.ruoyi.yipaiju.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ApiResult;
import com.ruoyi.common.core.page.TableDataApi;
import com.ruoyi.common.enums.dict.HouseCategory;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.yipaiju.domain.Notice;
import com.ruoyi.yipaiju.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/notices")
public class NoticesController extends BaseController {

    @Autowired
    private INoticeService noticesService;

    /**
     * 添加通知
     * @param notice
     * @return
     */
    @PostMapping("")
    public ApiResult add(@RequestBody Notice notice) {
        notice.setClient(SecurityUtils.getUserId().toString());
        notice.setClient(SecurityUtils.getUserId().toString());
        if (notice.getForeclosedHouseId() != null){
            notice.setNoticeType(HouseCategory.FORECLOSED.getValue());
        }
        if (notice.getDiscountHouseId() != null){
            notice.setNoticeType(HouseCategory.DISCOUNT.getValue());
        }
        noticesService.insertNotice(notice);
        return ApiResult.success(notice);
    }

    /**
     * 删除通知
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public TableDataApi remove(@PathVariable Long id)
    {
        noticesService.deleteNoticeById(id);
        startPageApi();
        List<Notice> notices = noticesService.selectNoticeHouseInfo(Builder.of(Notice::new).with(Notice::setClient, SecurityUtils.getUserId().toString()).build());
        return getTableApi(notices);
    }

    /**
     * 我的通知列表
     * @param notice
     * @return
     */
    @GetMapping("")
    public TableDataApi list(Notice notice) {
        notice.setClient(SecurityUtils.getUserId().toString());
        startPageApi();
        List<Notice> notices = noticesService.selectNoticeHouseInfo(notice);
//        TableDataApi tableDataApi = getTableApi(notices);
//        return ApiResult.success(ImmutableMap.of("results", tableDataApi.getRows(), "total", tableDataApi.getTotal()));
        return getTableApi(notices);
    }

}